package com.tanhua.bg.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tanhua.bg.mapper.UserInfoMapper;
import com.tanhua.bg.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zongjie
 * @Description: 用户信息service
 * @date 2021/10/13  11:47
 */
@Service
public class UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 查询用户数
     *
     * @param queryWrapper
     * @return
     */
    public Integer queryUserCount(QueryWrapper queryWrapper) {
        return userInfoMapper.selectList(queryWrapper).size();
    }


    public List<UserInfo> queryUserInfoByUserIdList(Collection<?> userIdList) {
        if (CollUtil.isEmpty(userIdList)) {
            return null;
        }
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userIdList);
        return this.queryUserInfoList(queryWrapper);
    }

    public List<UserInfo> queryUserInfoList(QueryWrapper<UserInfo> queryWrapper) {
        return this.userInfoMapper.selectList(queryWrapper);
    }
     /*根据用户id查询用户信息
                * @param userId
                * @return
     */
        public UserInfo queryUserInfoById(Long userId){
            QueryWrapper<UserInfo> query = new QueryWrapper<>();
            query.eq("user_id", userId);
            return userInfoMapper.selectOne(query);
        }

        /**
         *  修改用户状态
         * @param userId
         * @return
         */
        public Boolean updateUserStatus (Long userId){
            UserInfo userInfo = queryUserInfoById(userId);
            if (Integer.parseInt(userInfo.getUserStatus()) == 1) {
                UpdateWrapper<UserInfo> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("user_id", userId);
                updateWrapper.set("user_status", "2");
                userInfoMapper.update(userInfo, updateWrapper);
                return true;
            } else {
                UpdateWrapper<UserInfo> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("user_id", userId);
                updateWrapper.set("user_status", "1");
                userInfoMapper.update(userInfo, updateWrapper);
                return true;
            }
        }

        /**
         *  根据昵称查询userinfo信息
         * @param nickname
         * @return
         */
        public List<UserInfo> queryUserInfoByNickName (String nickname){
            Map<String, Object> map = new HashMap<>();
            map.put("nick_name", nickname);
            List<UserInfo> userInfos = userInfoMapper.selectByMap(map);
            return userInfos;
        }

        /**
         *  根据城市查询userinfo信息
         * @param city
         * @return
         */
        public List<UserInfo> queryUserInfoByCity (String city){
            Map<String, Object> map = new HashMap<>();
            map.put("city", city);
            List<UserInfo> userInfos = userInfoMapper.selectByMap(map);
            return userInfos;
        }

}